package main

import (
	"flag"
	"fmt"
	"log"

	"server-go/api/reward/internal/config"
	"server-go/api/reward/internal/handler"
	"server-go/api/reward/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
	_ "github.com/zeromicro/zero-contrib/zrpc/registry/consul"
)

var configFile = flag.String("f", "etc/reward.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	server := rest.MustNewServer(c.RestConf, rest.WithCors())
	defer server.Stop()

	ctx := svc.NewServiceContext(c)
	func() {
		// 用于启动队列
		if err := ctx.ProducerA.Start(); err != nil {
			log.Println("ProducerA队列启动失败, " + err.Error())
			panic("ProducerA队列启动失败, " + err.Error())
		}
	}()
	defer func() {
		// 用于关闭队列
		ctx.ProducerA.Shutdown()
	}()

	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
